﻿using System;
using System.Collections.Generic;

namespace EventAggregator
{
    public sealed class EventMessage : IDisposable
    {
        private readonly int _id;
        private readonly Dictionary<string, object> _parameters;

        public EventMessage(int id, params KeyValuePair<string, object>[] parameters)
        {
            _id = id;

            _parameters = new Dictionary<string, object>();

            if (parameters != null)
            {
                foreach (var keyValuePair in parameters)
                {
                    _parameters.Add(keyValuePair.Key, keyValuePair.Value);
                }
            }
        }

        public int Id
        {
            get { return _id; }
        }

        #region IDisposable Members

        public void Dispose()
        {
            _parameters.Clear();
        }

        #endregion

        public bool Contains(string key)
        {
            return _parameters.ContainsKey(key);
        }

        public T GetValue<T>(string key)
        {
            return (T) _parameters[key];
        }

        public void SetValue(string key, object value)
        {
            if (_parameters.ContainsKey(key))
            {
                _parameters.Remove(key);
            }

            _parameters.Add(key, value);
        }
    }
}